package com.yuqing.sentimentapp.home.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.linkface.common.adaper.CommonRecycleAdapter;
import com.linkface.common.adaper.CommonViewHolder;
import com.linkface.common.adaper.MultiTypeSupport;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.home.bean.NewsDataBean;
import com.yuqing.sentimentapp.home.bean.RecommendDataBean;
import com.yuqing.sentimentapp.utils.GlideUtil;
import com.yuqing.sentimentapp.utils.ToastUtil;

import java.util.List;

/**
 * Created by shenhe on 2018/9/19.
 */

public class NewsAdapter extends CommonRecycleAdapter<NewsDataBean.DataBean> implements MultiTypeSupport<NewsDataBean.DataBean> {
    private boolean isVip;
    private CommonViewHolder.onItemCommonClickListener commonClickListener;
    private Context mContext;

    public NewsAdapter(Context context, List<NewsDataBean.DataBean> dataList) {
        super(context, dataList, R.layout.fragment_news_item_no_pic);
        this.mContext = context;
    }

    public NewsAdapter(Context context, List<NewsDataBean.DataBean> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener) {
        super(context, dataList, R.layout.fragment_news_item_no_pic);
        this.mContext = context;
        this.commonClickListener = commonClickListener;
        this.multiTypeSupport = this;
    }

    public NewsAdapter(Context context, List<NewsDataBean.DataBean> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener, boolean isVip) {
        super(context, dataList, R.layout.fragment_news_item_no_pic);
        this.mContext = context;
        this.commonClickListener = commonClickListener;
        this.multiTypeSupport = this;
        this.isVip = isVip;
    }

    @Override
    public void bindData(final CommonViewHolder holder, final NewsDataBean.DataBean data) {
        holder.setText(R.id.tv_title, data.getTitle())
                .setText(R.id.tv_date, "发布时间：" + data.getCreateAt());
        String isRead = data.getIsRead();
        if (!TextUtils.isEmpty(isRead) && TextUtils.equals("1", isRead)) {
            ((TextView) holder.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_aaaaaa));
        } else {
            ((TextView) holder.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_333333));
        }
        String imageUrl = data.getImageUrl();
        if (!TextUtils.isEmpty(imageUrl)) {
            String[] images = imageUrl.split(";");
            if (images.length < 3){//一张图
                GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic), 3);
            } else {//三张图
                GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic_one), 3);
                GlideUtil.loadRoundImage(mContext, images[1], holder.getView(R.id.iv_pic_two), 3);
                GlideUtil.loadRoundImage(mContext, images[2], holder.getView(R.id.iv_pic_three), 3);
            }
        }
        if (isVip) {
            holder.setViewVisibility(R.id.iv_vip, View.VISIBLE);
        } else {
            holder.setViewVisibility(R.id.iv_vip, View.GONE);
        }
        holder.setCommonClickListener(commonClickListener);
    }

    @Override
    public int getLayoutId(NewsDataBean.DataBean item, int position) {
        String imageUrl = item.getImageUrl();
        if (TextUtils.isEmpty(imageUrl)) {
            return R.layout.fragment_news_item_no_pic;
        } else {
            String[] images = imageUrl.split(";");
            if (images.length == 0) {
                return R.layout.fragment_news_item_no_pic;
            } else if (images.length < 3) {
                return R.layout.fragment_news_item_one_pic;
            } else {
                return R.layout.fragment_news_item_three_pic;
            }
        }
    }
}
